/**
 * 33-权限-管理员组管理-列表. <br/>
 *
 * @author fsl fzh 20151127.
 */
$(function() {

    bindEvent();

    // 初始化，获取第一页数据.
    getAdminList(1);

});

/**
 * 在页面初始化时, 绑定事件.<br/>
 *
 * @author fzh 20151127
 */
function bindEvent() {

    //给添加管理组添加地址
    $("#add_admin").attr("href", PAGE_DATA.REST_API.new_admin_url)

    var tblBody = $("#admin-list-context");
    var selAllChk = $("#sel-all-chk");

    // 每行数据的, 选中效果
    tblBody.on("click", ".sel-chk", function() {
        var chkObj = $(this).find(":checkbox");
        var chkVal = chkObj.prop("checked");
        chkObj.prop("checked", !chkVal);

        var unChk = tblBody.find(":checkbox:not(:checked)").length;
        selAllChk.prop("checked", (unChk == 0));

        return;

    });

    // 默认冒泡事件.
    tblBody.on("click", ":checkbox", function(event) {
        // event.stopPropagation();
        var chkVal = $(this).prop("checked");
        $(this).prop("checked", !chkVal);

    });

    // 全选
    selAllChk.on("click", function() {
        var chkAllVal = $(this).prop("checked");
        tblBody.find(":checkbox").prop("checked", chkAllVal);

    });

    // 删除-多条数据.
    $("#del-multi-btn").on("click", function() {

        var that = this;

        var delIdAry = [];
        tblBody.find(":checked").each(function() {
            delIdAry.push($(this).attr("id"));
        });

        if (delIdAry.length == 0) {

            var conf = {
                title: "提示",
                message: "请选择需要删除的数据!",
                ok_txt: "确定"
            };
            adminUtil.showMsg(conf);

            return false;
        }

        // 删除多条数据.
        var delMulti = function() {
            // var delIds = delIdAry;
            var delMultiUrl = PAGE_DATA.REST_API.get_admin_list_url +
                "?page=" + $("#comm-paging").attr("current-page") + "&pageSize=10";
            delAdmin(delMultiUrl, delIdAry);
        };

        var conf = {
            title: "来自网页的消息",
            message: "删除后不可恢复，确定删除吗?",
            ok_func: delMulti,
            ok_context: that,
            ok_param: null
        };
        adminUtil.showConfDialog(conf);

    });

    // 删除单条数据
    tblBody.on("click", ".del-admin-btn", function() {

        var that = this;
        var delSingle = function() {
            var delHref = $(this).attr("href");
            delAdmin(delHref, "");
        };

        var conf = {
            title: "来自网页的消息",
            message: "删除后不可恢复，确定删除吗?",
            ok_func: delSingle,
            ok_context: that,
            ok_param: null
        };
        adminUtil.showConfDialog(conf);
    });

};
/**
 * 删除管理员数据. 单条 或 多条 都可.<br/>
 *
 * @param restApi {string} REST API
 * @param adminID {array} 管理员ID
 *
 * @author fsl-fzh-20151127.
 */
function delAdmin(restApi, adminID) {

    $.ajax({
        url: restApi,
        type: "put",
        data: {
            ids: adminID || '',
            _token: COMM_DATA.csrfToken
        },
        success: function(result) {
            // 重新渲染列表
            renderList(result);
        },
        error: function(result) {
            // Object {readyState: 4, responseText: "{"data":"\u5bc6\u7801\u9519\u8bef"}",
            // responseJSON: Object, status: 403, statusText: "Forbidden"}

            var errorMsg = "";
            if (typeof result.responseJSON != 'undefined' && typeof result.responseJSON.data != 'undefined') {
                errorMsg = result.responseJSON.data;
            }

            var conf = {
                title: "提示",
                message: errorMsg || "获取数据失败!",
                ok_txt: "确定"
            };

            adminUtil.showMsg(conf);
        }

    });
};

/**
 * 获取管理员列表数据.<br/>
 * @param page {int} 查询页码.
 *
 * @author fsl-fzh-20151127.
 */
function getAdminList(page) {

    var brandListTempl = _.template($('#adminListTemp').html());

    $.ajax({
        url: PAGE_DATA.REST_API.get_admin_list_url || '',
        type: "get",
        data: {
            page: parseInt(page || 0),
            pageSize: 10,
        },
        success: function(result) {
            renderList
                (result);
        },
        error: function(status, result) {
            var conf = {
                title: "提示",
                message: "获取数据失败!",
                ok_txt: "确定"
            };
            adminUtil.showMsg(conf);
        }
    });
};
/**
 * 渲染列表数据, 翻页.
 *
 * @param ajaxResult {object} ajax请求返回的数据对象.
 *
 * @author fsl-fzh-20151127
 */
function renderList(ajaxResult) {

    var onClickObj = {
        onClick: function() {
            var clickPage = $(this).attr("data-page");
            getAdminList(clickPage);
        }
    };

    var dataList = $.extend([], ajaxResult.data);

    //  表格数据模板.
    var tmplObj = _.template($("#adminListTemp").html());

    var dataHtml = tmplObj(dataList);
    $("#admin-list-context").html(dataHtml);

    // 翻页.
    var defaultPagingObj = {
        total: 0,
        pageSize: 0,
        page: 0
    };

    var pagingConf = $.extend(defaultPagingObj, ajaxResult.pagingConfData, onClickObj);

    adminUtil.initPaging('#comm-paging', pagingConf);

    // 重置全选.
    $("#sel-all-chk").prop("checked", false);

};
